using System;
using System.Collections.Generic;
using System.Text;
using SmartContentDB;

namespace SmartContentBusiness
{
    public class Session
    {
        #region Variables
        private SmartContentDB.Interfaces.User user;
        private string sessionId;
        private DBAccess dbaccess;
        #endregion

        #region Constructor
        public Session(string server, string port, string schema, string user, string password)
        {
            this.sessionId = null;
            dbaccess = new DBAccess(DBAccess.DBMotor.MySql, user, password, schema, server, port);
            dbaccess.connectDB();
        }
        #endregion

        #region Public Methods
        public bool doLogin(string userName, string password)
        {
            if((user = this.dbaccess.doLogin(userName, password)) != null)
                this.sessionId = Guid.NewGuid().ToString();
            return this.sessionId != null;
        }

        public bool doLogout()
        {
            this.dbaccess.doLogout();
            return true;
        }

        public SmartContentDB.Interfaces.Content[] getContent(int parentId)
        {
            return this.dbaccess.getContent(parentId);
        }

        public SmartContentDB.Interfaces.Content getFullContent(int contentId)
        {
            return this.dbaccess.getFullContent(contentId);
        }

        public SmartContentDB.Interfaces.Field[] executeQuery(string table, int tableId, string where)
        {
            return this.dbaccess.executeQuery(table, tableId, user.getId(), where);
        }

        public SmartContentDB.Interfaces.Document getRow(SmartContentDB.Interfaces.Field[] column)
        {
            return this.dbaccess.getRow(column);
        }

        public void releaseQuery()
        {
            this.dbaccess.releaseQuery();
        }

        public void addDocument(SmartContentDB.Interfaces.Field[] column, string[] values, string table, SmartContentDB.Interfaces.UserRight[] rights)
        {
            this.dbaccess.addDocument(column, values, table, this.user.getUser(), rights);
        }
        
        public void delDocument(string table, int tableId, string documentId)
        {
            this.dbaccess.delDocument(table, tableId, documentId);
        }

        public void addUser(SmartContentDB.Interfaces.User user)
        {
            this.dbaccess.addUser(user);
        }

        public SmartContentDB.Interfaces.User[] getUsers()
        {
            if (user.getClientType() == SmartContentDB.Interfaces.User.ClientType.Administrator)
            {
                return this.dbaccess.getUsers();
            }
            else
            {
                return new SmartContentDB.Interfaces.User[] { user };
            }
        }
        public SmartContentDB.Interfaces.User[] getUserList()
        {
            return this.dbaccess.getUserList();
        }
        public SmartContentDB.Interfaces.UserRight getUserRights(int userId, int containerId, int documentId)
        {
            return this.dbaccess.getUserRights(userId, containerId, documentId);
        }

        public void delUser(SmartContentDB.Interfaces.User user)
        {
            this.dbaccess.delUser(user);
        }

        public void addContainer(SmartContentDB.Interfaces.Content content)
        {
            this.dbaccess.addContainer(content);
        }

        public void updContent(SmartContentDB.Interfaces.Content content)
        {
            this.dbaccess.updContent(content);
        }

        public void addRegistry(SmartContentDB.Interfaces.Content content, SmartContentDB.Interfaces.Field[] fields)
        {
            this.dbaccess.addRegistry(content, fields);
        }

        public void addRegistryContent(string name,int registryId, SmartContentDB.Interfaces.Content content)
        {
            this.dbaccess.addRegistryContent(name,registryId, content);
        }

        public SmartContentDB.Interfaces.Content[] getRegistryContent(string table, int registryId, int parentId)
        {
            return this.dbaccess.getRegistryContent(table, registryId, parentId);
        }
        /*public void getFilteredContent(String busqueda, System.Data.DataSet datosALlenar)
        {
            this.dbaccess.getFilteredContent(busqueda, datosALlenar);
        }*/
        public System.Collections.ArrayList getNombresColumnas(string table)
        {
            return this.dbaccess.getNombresColumnas(table);
        }
        public void updateDocument(string table, int tableId, SmartContentDB.Interfaces.Field[] columns, string[] values,SmartContentDB.Interfaces.UserRight[] rights, int id)
        {
            this.dbaccess.updateDocument(table, tableId, columns, values, rights, id);
        }
        #endregion

        #region Set/Get
        public string getId()
        {
            return sessionId;
        }

        public SmartContentDB.Interfaces.User getUser()
        {
            return this.user;
        }
        #endregion
    }
}
